Skip to content

Method: inferObjectPropertyValues(OWLNamedIndividual, Assertion)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.model.*;
18: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
19: import cz.cvut.kbss.ontodriver.owlapi.exception.ReasonerNotAvailableException;
20: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
21: import org.semanticweb.owlapi.model.*;
22: import org.semanticweb.owlapi.reasoner.OWLReasoner;
23:
24: import java.util.*;
25: import java.util.stream.Collectors;
26: import java.util.stream.Stream;
27:
28: public class InferredAxiomLoader implements AxiomLoader {
29:
30: private final OWLReasoner reasoner;
31: private final OWLOntology ontology;
32: private final OWLDataFactory dataFactory;
33:
34: private final OwlapiAdapter adapter;
35: private final AxiomAdapter axiomAdapter;
36:
37: private NamedResource subject;
38:
39: InferredAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
40: this.adapter = adapter;
41: this.reasoner = snapshot.getReasoner();
42: this.ontology = snapshot.getOntology();
43: this.dataFactory = snapshot.getDataFactory();
44: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory());
45: }
46:
47: @Override
48: public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
49: this.subject = subject;
50: if (assertions.isEmpty()) {
51: return Collections.emptySet();
52: }
53: if (reasoner == null) {
54: throw new ReasonerNotAvailableException();
55: }
56: reasoner.flush();
57: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
58: final Collection<Axiom<?>> axioms = new HashSet<>();
59: for (Assertion a : assertions) {
60: switch (a.getType()) {
61: case CLASS:
62: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
63: break;
64: case DATA_PROPERTY:
65: axioms.addAll(inferDataPropertyValues(individual, a));
66: break;
67: case OBJECT_PROPERTY:
68: axioms.addAll(inferObjectPropertyValues(individual, a));
69: break;
70: case PROPERTY:
71: // When we don't know, try all
72: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
73: axioms.addAll(inferDataPropertyValues(individual, a));
74: axioms.addAll(inferObjectPropertyValues(individual, a));
75: break;
76: default:
77: break;
78: }
79: }
80: return axioms;
81: }
82:
83: private Collection<Axiom<?>> inferDataPropertyValues(OWLNamedIndividual individual, Assertion dpAssertion) {
84: final Set<OWLLiteral> literals = reasoner.getDataPropertyValues(individual, dataProperty(dpAssertion));
85: return literals.stream().filter(lit -> OwlapiUtils.doesLanguageMatch(lit, dpAssertion))
86: .map(owlLiteral -> new AxiomImpl<>(subject, dpAssertion,
87: new Value<>(OwlapiUtils.owlLiteralToValue(owlLiteral)))).collect(Collectors.toSet());
88: }
89:
90: private OWLDataProperty dataProperty(Assertion dataPropertyAssertion) {
91: return dataFactory.getOWLDataProperty(IRI.create(dataPropertyAssertion.getIdentifier()));
92: }
93:
94: private Collection<Axiom<?>> inferObjectPropertyValues(OWLNamedIndividual individual, Assertion opAssertion) {
95: final Stream<OWLNamedIndividual> individuals =
96: reasoner.getObjectPropertyValues(individual, objectProperty(opAssertion)).entities();
97: return individuals.map(
98: target -> axiomAdapter.createAxiom(subject, opAssertion, NamedResource.create(target.getIRI().toURI())))
99: .collect(
100: Collectors.toList());
101: }
102:
103: private OWLObjectProperty objectProperty(Assertion objectPropertyAssertion) {
104: return dataFactory.getOWLObjectProperty(IRI.create(objectPropertyAssertion.getIdentifier()));
105: }
106:
107: @Override
108: public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
109: final Collection<Axiom<?>> axioms = new ArrayList<>();
110: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
111: ontology.dataPropertiesInSignature().forEach(dp -> {
112: final Set<OWLLiteral> values = reasoner.getDataPropertyValues(individual, dp);
113: for (OWLLiteral literal : values) {
114: axioms.add(axiomAdapter.createAxiom(subject,
115: Assertion.createDataPropertyAssertion(dp.getIRI().toURI(), true), literal));
116: }
117: });
118: ontology.objectPropertiesInSignature().forEach(op -> {
119: final Assertion opAss = Assertion.createObjectPropertyAssertion(op.getIRI().toURI(), true);
120: reasoner.getObjectPropertyValues(individual, op).entities()
121: .forEach(ind -> axioms
122: .add(axiomAdapter.createAxiom(subject, opAss, NamedResource.create(ind.getIRI().toURI()))));
123: });
124: return axioms;
125: }
126: }